package designpatterns.creation;

import java.math.BigDecimal;

/**
 * 抽象工厂 将创建对象和使用对象分离、相对于普通工厂模式更能符合开闭原则
 *
 * @author jack.wu
 * @since 2019/9/18
 */
public class AbstractFactoryTest {

    public static void main(String[] args) {
        Product pen = new PenCreateFactory().createProduct("白色", new BigDecimal(3));
        pen.show();
        Product waterCup = new WaterCupCreateFactory().createProduct("蓝色", new BigDecimal(18));
        waterCup.show();
    }

}

interface Product {
    /**
     * 显示产品
     */
    void show();
}

class Pen implements Product {

    private String color;
    private BigDecimal price;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @Override
    public void show() {
        System.out.println("产品：钢笔，颜色：" + color + ",价格：" + price);
    }
}

class WaterCup implements Product {

    private String color;
    private BigDecimal price;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @Override
    public void show() {
        System.out.println("产品：水杯，颜色：" + color + ",价格：" + price);
    }
}

interface AbstractFactory {

    /**
     * 创建产品
     *
     * @param color 产品颜色
     * @param price 产品价格
     * @return 产品
     */
    Product createProduct(String color, BigDecimal price);
}

class PenCreateFactory implements AbstractFactory {

    @Override
    public Product createProduct(String color, BigDecimal price) {
        Pen pen = new Pen();
        pen.setColor(color);
        pen.setPrice(price);
        return pen;
    }
}

class WaterCupCreateFactory implements AbstractFactory {

    @Override
    public Product createProduct(String color, BigDecimal price) {
        WaterCup waterCup = new WaterCup();
        waterCup.setColor(color);
        waterCup.setPrice(price);
        return waterCup;
    }
}

